package top.jolyoulu.core.acc

import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator
import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

/**
 * @Author: JolyouLu
 * @Date: 2024/2/6 16:32
 * @Description
 */
object Spark01_RDD_Bc {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Acc")
    val sc: SparkContext = new SparkContext(sparkConf)

    val rdd1: RDD[(String,Int)] = sc.makeRDD(List(
      ("a",1),
      ("b",2),
      ("c",3),
    ))
    val map = mutable.Map(("a",4), ("b",5), ("c",6))
    //封装广播变量
    val bc: Broadcast[mutable.Map[String, Int]] = sc.broadcast(map)

    rdd1.map{
      case(w,c) => {
        val l: Int = bc.value.getOrElse(w, 0)
        (w,(c,l))
      }
    }.collect().foreach(println)

    //关闭环境
    sc.stop()
  }
}
